package com.xiaoshuidi.cloud.module.rooms.valid;

import com.xiaoshuidi.cloud.module.rooms.annotation.ExcelValid;
import com.xiaoshuidi.cloud.module.rooms.controller.admin.roomequipment.vo.PartLeaseEquipmentTemplateVo;
import com.xiaoshuidi.cloud.module.rooms.controller.admin.roomequipment.vo.WholeLeaseEquipmentTemplateVo;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;
import java.util.Objects;

public class PartLeaseEquipmentImportValid extends AbstractEquipmentExcelImportValid<PartLeaseEquipmentTemplateVo> {
    @Override
    public boolean valid(PartLeaseEquipmentTemplateVo part, Map<String, String> errorMap) throws Exception {
        Field[] fields = part.getClass().getDeclaredFields();
        Field[] supperFields = part.getClass().getSuperclass().getDeclaredFields();
        ArrayList<Field> list = new ArrayList<>(Arrays.asList(supperFields));
        list.addAll(Arrays.asList(fields));

        for (Field field : list) {
            field.setAccessible(true);
            //属性的值
            Object fieldValue = null;
            String fieldName = field.getName();
            try {
                fieldValue = field.get(part);
            } catch (IllegalAccessException e) {
                errorMap.put("exception", field.getAnnotation(ExcelValid.class).message());
                return false;

            }
            boolean isValid = field.isAnnotationPresent(ExcelValid.class);
            if (isValid && Objects.isNull(fieldValue)) {
                errorMap.put("exception", field.getAnnotation(ExcelValid.class).message());
                return false;
            }

            boolean result = validEquiptmentImportItem(fieldName, fieldValue, errorMap);
            if (!result) {
                return result;
            }
        }
        return true;
    }
}
